/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */

package com.nai365.nart.operation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.common.log.annotation.SysLog;
import com.nai365.nart.common.security.util.SecurityUtils;
import com.nai365.nart.operation.dto.PledgeParamDto;
import com.nai365.nart.operation.dto.PledgeRecordDto;
import com.nai365.nart.operation.entity.PledgeRecord;
import com.nai365.nart.operation.service.PledgeRecordService;
import com.nai365.nart.product.entity.SkuInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 质押记录
 *
 * @author linco
 * @date 2022-07-01 11:13:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/pledge")
@Api(value = "pledge", tags = "质押")
public class PledgeController {

	private final PledgeRecordService pledgeRecordService;

	/**
	 * 分页查询
	 *
	 * @param page         分页对象
	 * @param pledgeRecord 质押记录
	 * @return R<Page < PledgeRecordDto>>
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R<Page<PledgeRecordDto>> getPledgeRecordPage(Page<PledgeRecord> page, PledgeRecord pledgeRecord) {
		QueryWrapper<PledgeRecord> queryWrapper = Wrappers.query(pledgeRecord);
		return R.ok(pledgeRecordService.page(page, queryWrapper));
	}


	/**
	 * 通过id查询质押记录
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	public R<PledgeRecordDto> getById(@PathVariable("id") Long id) {
		return R.ok(pledgeRecordService.getPledgeRecordDtoById(id));
	}


	@ApiOperation(value = "质押", notes = "质押")
	@SysLog("质押")
	@PostMapping
	public R<PledgeRecordDto> pledge(@RequestBody PledgeParamDto pledgeParamDto) {
		try {
			return R.ok(pledgeRecordService.pledge(pledgeParamDto), "质押成功！");
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "可质押的SKU", notes = "可质押的SKU")
	@SysLog("可质押的SKU")
	@GetMapping("/sku")
	public R<List<SkuInfo>> sku(@RequestParam Long pledgeDemandId) {
		return R.ok(pledgeRecordService.findAblePledgeSku(pledgeDemandId));
	}

	@ApiOperation(value = "我的质押记录", notes = "我的质押记录")
	@SysLog("可质押的SKU")
	@GetMapping("/record/my")
	public R<Page<PledgeRecordDto>> myPledgeRecord(Page<PledgeRecord> page, Integer status) {
		QueryWrapper<PledgeRecord> queryWrapper = Wrappers.query();
		queryWrapper.eq("user_id", SecurityUtils.getUser().getId());
		if (status != null) {
			queryWrapper.eq("status", status);
		}
		return R.ok(pledgeRecordService.page(page, queryWrapper));
	}

	@ApiOperation(value = "取消质押", notes = "取消质押")
	@SysLog("可质押的SKU")
	@GetMapping("/cancel")
	public R<PledgeRecordDto> cancel(@RequestParam Long pledgeRecordId) {
		try {
			return R.ok(this.pledgeRecordService.cancel(pledgeRecordId), "取消质押成功");
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

}
